package org.flashbrighton.as3bots.components
{
	import flash.display.*;
	import flash.filters.GlowFilter;
	import org.flashbrighton.as3bots.models.BotModel;
	import org.flashbrighton.as3bots.views.GameView;
	
	public class ChassisComponent implements IComponent
	{
		public var density:Number;
		public var friction:Number;
		public var restitution:Number;
		public var linDamping:Number;
		public var angDamping:Number;
		
		
		public function ChassisComponent()
		{
			setDefaults();
		}
		
		
		public function update(botModel:BotModel):void 
		{
		}
		
		
		public function getRadiusFromSize(chassisSize:String):Number
		{
			var radius:Number;
			
			switch (chassisSize)
			{
				case ChassisSize.SMALL:
				radius = 0.25;
				break;
				
				case ChassisSize.MEDIUM:
				radius = 0.3;
				break;
				
				case ChassisSize.LARGE:
				radius = 0.35;
				break;
			}
			
			return radius;
		}
		
		
		public function createSkin(size:String, colour:Number, image:Sprite=null):Sprite
		{
			var radius:Number = getRadiusFromSize(size);
			var skin:Sprite;
			
			if (image)
			{
				skin = image;
			}
			else
			{
				skin = new Sprite();
				skin.addChild(drawDefaultShape(colour));
			}
			
			skin.width = GameView.SCALE_FACTOR * (radius * 2);
			skin.height = GameView.SCALE_FACTOR * (radius * 2);
			
			return skin;
		}
		
		
		private function drawDefaultShape(lineColour:Number=0xFFFFFF):Shape
		{
			var circle:Shape = new Shape();
			var g:Graphics = circle.graphics;
			
			g.lineStyle(2, lineColour, 1, false, LineScaleMode.NONE);
			g.drawCircle(0, 0, 10);
			g.moveTo(0, 0);
			g.lineTo(10, 0);
			
			circle.filters = [new GlowFilter(lineColour, 0.5, 8, 8, 2)];
			
			return circle;
		}
		
		
		private function setDefaults():void 
		{
			density = 10;
			friction = 0.3;
			restitution = 0.1;
			
			// Top-down friction
			linDamping = 27;
			angDamping = 27;
		}
		
	}
}